package com.example.keeppwd2;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.hardware.fingerprint.FingerprintManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

	public WebView wv1;
	private WebSettings mWebSettings;
	public String IKEY;

	public final Handler myHandler = new Handler();
	public SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
	public BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
	private int token = 0;

	public Date unlockdt = new Date();

	public void quit() {
		moveTaskToBack(false);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWv();
	}

	@SuppressLint("SetJavaScriptEnabled")
	@SuppressWarnings("deprecation")
	private void initWv() {
		wv1 = new WebView(this);
		wv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		setContentView(wv1);
		soundPool.load(this, R.raw.kr, 1);

		mWebSettings = wv1.getSettings();
		mWebSettings.setJavaScriptEnabled(true);
		mWebSettings.setBuiltInZoomControls(false);
		mWebSettings.setSupportZoom(false);
		mWebSettings.setAllowFileAccessFromFileURLs(true);
		mWebSettings.setAllowUniversalAccessFromFileURLs(true);

		mWebSettings.setDomStorageEnabled(true);
		String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
		mWebSettings.setAppCachePath(appCachePath);
		mWebSettings.setAllowFileAccess(false);

		mWebSettings.setAppCacheEnabled(true);
		wv1.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				return true;
			}
		});
		wv1.setHapticFeedbackEnabled(false);

		final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);
		UUID uuid = UUID.randomUUID();
		IKEY = uuid.toString();
		token = 1;
		wv1.addJavascriptInterface(myJavaScriptInterface, "androidFunction");

		wv1.loadUrl("file:///android_asset/start/index.html?IKey=" + IKEY + "&NotShowPwd=1&NotPWA=1&IsSupportFingerprint="
				+ (FingerprintHelper.IsSupportFingerprint(this) ? "1" : "0"));
		// checkConn();
	}

	@Override
	public void onResume() {
		super.onResume();
		if (wv1 == null) {
			initWv();
		} else {
			// checkConn();
			if (token != 1) {
				Date dt2 = new Date();
				if (dt2.getTime() - unlockdt.getTime() >= 800)
					wv1.loadUrl("javascript:IFLock('" + IKEY + "')");
			}
		}
		token = 2;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			wv1.loadUrl("javascript:IFGoBack('" + IKEY + "')");
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

}
